﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo;

using DevExpress.Utils;
using DevTeam;


using System;
using System.Drawing;
using System.Windows.Forms;




namespace ConfExpert.Forms.Du4
{
    public partial class StokDurumu : XForm
    {
        public StokDurumu()
        {
            InitializeComponent();
        }

        BindingSource bs, bsSip, bsTop;
        private void Consumption_Load(object sender, EventArgs e)
        {

            ds.xDU4ihtiyac.Columns.Add("RaporBoyu", typeof(decimal));

            bs = new BindingSource(ds, "xDU4ihtiyac");
            bsSip = new BindingSource(bs, "FK_xDU4ihtiyacSip_xDU4ihtiyac_ProductId");
            bsTop = new BindingSource(bs, "FK_xDU4ihtiyacTop_xDU4ihtiyac_ProductId");

            InitGridView(gridView1, bs);
            InitGridView(gridView2, bsSip);
            InitGridView(gridView3, bsTop);
            //gridView3.FormatAsNumber(2, "Qty");
            gridView1.MakeReadOnly("RaporBoyu");
            gridView2.OptionsBehavior.Editable = gridView3.OptionsBehavior.Editable = false;

            gridView2.MakeColumnInvisible("ProductId");
            gridView2.MakeColumnInvisible("Scan", "ProductId");

            gridView1.AddSummary("QtyNeeded", "QtyDepo", "BlockedQty", "AvailQty");
            gridView2.AddSummary("QtyNeeded", "ProdPrice", "BlockedQty", "TotalPerPlan");
            gridView3.AddSummary("Qty");

            gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);
            gridView2.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);
            gridView3.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);

            splitContainerControl1.AutoSize();
            splitContainerControl2.AutoSize();

            gridView1.ColumnFilterChanged += gridView1_ColumnFilterChanged;
            bs.CurrentChanged += new EventHandler(bs_CurrentChanged);
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            btnRaporBoyuKaydet.ItemClick += btnRaporBoyuKaydet_ItemClick;
            btnGuncelle.PerformClick();

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Bu artikelin HAREKETLERİ", HareketleriGoster);

            gridView1.FormatAsNumber(1, "QtyNeeded", "QtyDepo", "BlockedQty", "AvailQty");
            gridView2.FormatAsNumber(1, "QtyNeeded", "BlockedQty", "ProdPrice");
            gridView3.FormatAsNumber(1, "Qty");

        }

        void gridView1_ColumnFilterChanged(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount == 1)
                BeginInvoke(new InvokeDelegate(InvokeMethod));
        }

        public delegate void InvokeDelegate();
        private void InvokeMethod()
        {
            gridView1.FocusedRowHandle = 0;
        }

        void btnRaporBoyuKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExecuteNonQuery("UpdRaporBoyuDu4", ds.xDU4ihtiyac.SerializeChanges());
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ds.Clear();
            GetDetails("xDU4ihtiyac;xDU4ihtiyacSip;xDU4ihtiyacTop", "GetDu4ihtiyac", memoExEdit1.EditValue.ToStr(), memoExEdit2.EditValue.ToStr());
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();

            gridView1.Columns["MusteriKodu"].Width = 250;
        }


        void HareketleriGoster(object sender, EventArgs e)
        {
            int productId = bs.GetCurrentValue("ProductId").ToInt(-1);
            if (productId > 0)
                using (DepoStokHareketler rzv = new DepoStokHareketler(productId))
                {
                    rzv.ShowDialog();
                }
        }

        void bs_CurrentChanged(object sender, EventArgs e)
        {
            gridView2.BestFitColumns();
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "MusteriKodu" || e.Column.FieldName == "Qty")
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }
    }
}